package network.udp.chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class UdpUserB {

    public static void main(String[] args) throws IOException {
        //UDP初始准备，数据包Socket，控制台reader，数据包packet，数据包存储空间buffer
        DatagramSocket socket = new DatagramSocket(7777);
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        DatagramPacket packet = null;
        byte[] dataBytes = null;
        String data = null;

        while (true) {
            data = reader.readLine();
            dataBytes = data.getBytes(StandardCharsets.UTF_8);

            packet = new DatagramPacket(dataBytes, 0, dataBytes.length,new InetSocketAddress("localhost",8888));
            socket.send(packet);

            if (data.equals("bye")) {
                break;
            }
        }
        socket.close();
    }
}
